home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.05 May 93 / Getting Started / Modeless.c next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  8.7 KB  |  468 lines  |  [TEXT/KAHL]

  1. /****************************************************/
  2. /*                                                    */
  3. /*  Modeless Code                                    */
  4. /*                                                  */
  5. /*    Copyright 1993, Dave Mark, All rights reserved     */
  6. /*    This code is proprietary property of Dave Mark    */
  7. /*    and may not be copied, distributed, etc.        */
  8. /*                                                    */    
  9. /****************************************************/
  10.  
  11. #define kBaseResID            128
  12. #define kAboutALRTid        129
  13. #define kDialogResID        128
  14.  
  15. #define kVisible            true
  16. #define    kMoveToBack            NULL
  17. #define    kMoveToFront        (WindowPtr)-1L
  18. #define kNoGoAway            false
  19. #define kSleep                60L
  20.  
  21. #define kOn                    1
  22. #define kOff                0
  23.  
  24. #define iAfghan                1
  25. #define iElephant            2
  26. #define iSquirrel            3
  27.  
  28. #define kLeftMargin            5
  29. #define kTopMargin            40
  30.  
  31. #define kFirstRadio            1
  32. #define kLastRadio            3
  33.  
  34. #define mApple                kBaseResID
  35. #define iAbout                1
  36.  
  37. #define mFile                kBaseResID+1
  38. #define iSettings            1
  39. #define iQuit                3
  40.  
  41.  
  42. /*************/
  43. /*  Globals  */
  44. /*************/
  45.  
  46. Boolean        gDone;
  47. short        gCurrentPICT = kBaseResID;
  48. DialogPtr    gSettingsDLOG = NULL;
  49. WindowPtr    gFredWindow = NULL;
  50.  
  51.  
  52. /***************/
  53. /*  Functions  */
  54. /***************/
  55.  
  56. void        ToolBoxInit( void );
  57. PicHandle    LoadPICT( short picID );
  58. void        CreateWindow( void );
  59. void        MenuBarInit( void );
  60. void        EventLoop( void );
  61. void        DoEvent( EventRecord *eventPtr );
  62. void        DoDialogEvent( EventRecord *eventPtr );
  63. void        HandleMouseDown( EventRecord *eventPtr );
  64. void        HandleMenuChoice( long menuChoice );
  65. void        HandleAppleChoice( short item );
  66. void        HandleFileChoice( short item );
  67. void        DoUpdate( EventRecord *eventPtr );
  68. void        CreateDialog( void );
  69. void        FlipControl( ControlHandle control );
  70. void        SwitchPICT( void );
  71.     
  72.  
  73. /******************************** main *********/
  74.  
  75. void    main( void )
  76. {
  77.     ToolBoxInit();
  78.     MenuBarInit();
  79.     
  80.     CreateWindow();
  81.     
  82.     EventLoop();
  83. }
  84.  
  85.  
  86. /*********************************** ToolBoxInit */
  87.  
  88. void    ToolBoxInit( void )
  89. {
  90.     InitGraf( &thePort );
  91.     InitFonts();
  92.     InitWindows();
  93.     InitMenus();
  94.     TEInit();
  95.     InitDialogs( NULL );
  96.     InitCursor();
  97. }
  98.  
  99.  
  100. /******************************** LoadPICT *********/
  101.  
  102. PicHandle    LoadPICT( short picID )
  103. {
  104.     PicHandle    pic;
  105.     
  106.     pic = GetPicture( picID );
  107.     
  108.     if ( pic == NULL )
  109.     {
  110.         SysBeep( 10 );    /*  Couldn't load the PICT resource!!!  */
  111.         ExitToShell();
  112.     }
  113. }
  114.  
  115.  
  116. /******************************** CreateWindow *********/
  117.  
  118. void    CreateWindow( void )
  119. {
  120.     PicHandle    pic;
  121.     Rect        r;
  122.     
  123.     pic = LoadPICT( gCurrentPICT );
  124.     
  125.     r = (**pic).picFrame;
  126.     
  127.     OffsetRect( &r, kLeftMargin - r.left,
  128.                     kTopMargin - r.top );
  129.     
  130.     gFredWindow = NewWindow( NULL, &r, "\pMy Pet Fred", kVisible,
  131.             noGrowDocProc, kMoveToBack, kNoGoAway, 0L );
  132.     
  133.     if ( gFredWindow == NULL )
  134.     {
  135.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  136.         ExitToShell();
  137.     }
  138.     
  139.     ShowWindow( gFredWindow );
  140.     SetPort( gFredWindow );
  141. }
  142.  
  143.  
  144. /****************** MenuBarInit ***********************/
  145.  
  146. void    MenuBarInit( void )
  147. {
  148.     Handle            menuBar;
  149.     MenuHandle        menu;
  150.     
  151.     menuBar = GetNewMBar( kBaseResID );
  152.     SetMenuBar( menuBar );
  153.  
  154.     menu = GetMHandle( mApple );
  155.     AddResMenu( menu, 'DRVR' );
  156.     
  157.     DrawMenuBar();
  158. }
  159.  
  160.  
  161. /******************************** EventLoop *********/
  162.  
  163. void    EventLoop( void )
  164. {        
  165.     EventRecord        event;
  166.     
  167.     gDone = false;
  168.     while ( gDone == false )
  169.     {
  170.         if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
  171.             DoEvent( &event );
  172.     }
  173. }
  174.  
  175.  
  176. /************************************* DoEvent     */
  177.  
  178. void    DoEvent( EventRecord *eventPtr )
  179. {
  180.     char        theChar;
  181.     
  182.     if ( IsDialogEvent( eventPtr ) )
  183.     {
  184.         DoDialogEvent( eventPtr );
  185.     }
  186.     else
  187.     {
  188.         switch ( eventPtr->what )
  189.         {
  190.             case mouseDown: 
  191.                 HandleMouseDown( eventPtr );
  192.                 break;
  193.             case keyDown:
  194.             case autoKey:
  195.                 theChar = eventPtr->message & charCodeMask;
  196.                 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  197.                     HandleMenuChoice( MenuKey( theChar ) );
  198.                 break;
  199.             case updateEvt:
  200.                 DoUpdate( eventPtr );
  201.                 break;
  202.         }
  203.     }
  204. }
  205.  
  206.  
  207. /************************************* DoDialogEvent     */
  208.  
  209. void    DoDialogEvent( EventRecord *eventPtr )
  210. {
  211.     short        itemHit;
  212.     short        itemType;
  213.     Handle        itemHandle;
  214.     Rect        itemRect;
  215.     short        curRadioButton, i;
  216.     char        theChar;
  217.     Boolean        becomingActive;
  218.     MenuHandle    menu;
  219.     DialogPtr    dialog;
  220.     
  221.     menu = GetMHandle( mFile );
  222.  
  223.     switch ( eventPtr->what )
  224.     {
  225.         case keyDown:
  226.         case autoKey:
  227.             theChar = eventPtr->message & charCodeMask;
  228.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  229.                 HandleMenuChoice( MenuKey( theChar ) );
  230.             break;
  231.         case activateEvt:
  232.             becomingActive = ( (eventPtr->modifiers & activeFlag) == activeFlag );
  233.             
  234.             if ( becomingActive )
  235.             {
  236.                 for ( i=kFirstRadio; i<=kLastRadio; i++ )
  237.                 {
  238.                     GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
  239.                     HiliteControl( (ControlHandle)itemHandle, 0 );
  240.                 }
  241.                 DisableItem( menu, iSettings );
  242.             }
  243.             else
  244.             {
  245.                 for ( i=kFirstRadio; i<=kLastRadio; i++ )
  246.                 {
  247.                     GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
  248.                     HiliteControl( (ControlHandle)itemHandle, 255 );
  249.                 }
  250.                 EnableItem( menu, iSettings );
  251.             }
  252.             break;
  253.     }
  254.         
  255.     if ( DialogSelect( eventPtr, &dialog, &itemHit ) )
  256.     {
  257.         switch ( itemHit )
  258.         {
  259.             case iAfghan:
  260.             case iElephant:
  261.             case iSquirrel:
  262.                 curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
  263.     
  264.                 if ( curRadioButton != itemHit )
  265.                 {
  266.                     GetDItem( dialog, curRadioButton, &itemType,
  267.                             &itemHandle, &itemRect );
  268.                     FlipControl( (ControlHandle)itemHandle );
  269.                     
  270.                     GetDItem( dialog, itemHit, &itemType,
  271.                             &itemHandle, &itemRect );
  272.                     FlipControl( (ControlHandle)itemHandle );
  273.                     
  274.                     curRadioButton = itemHit;
  275.                     
  276.                     if ( gCurrentPICT != curRadioButton +
  277.                             kBaseResID - kFirstRadio )
  278.                     {
  279.                         gCurrentPICT = curRadioButton +
  280.                                 kBaseResID - kFirstRadio;
  281.                         SwitchPICT();
  282.                     }
  283.                 }
  284.                 break;
  285.         }
  286.     }
  287. }
  288.  
  289.  
  290. /************************************* HandleMouseDown */
  291.  
  292. void    HandleMouseDown( EventRecord *eventPtr )
  293. {
  294.     WindowPtr        window;
  295.     short            thePart;
  296.     long            menuChoice;
  297.     GrafPtr            oldPort;
  298.     long            windSize;
  299.     Rect            growRect;
  300.     MenuHandle        menu;
  301.     
  302.     thePart = FindWindow( eventPtr->where, &window );
  303.     
  304.     switch ( thePart )
  305.     {
  306.         case inMenuBar:
  307.             menuChoice = MenuSelect( eventPtr->where );
  308.             HandleMenuChoice( menuChoice );
  309.             break;
  310.         case inSysWindow : 
  311.             SystemClick( eventPtr, window );
  312.             break;
  313.         case inContent:
  314.             SelectWindow( window );
  315.             break;
  316.         case inDrag : 
  317.             DragWindow( window, eventPtr->where, &screenBits.bounds );
  318.             break;
  319.         case inGoAway:
  320.             if ( TrackGoAway( window, eventPtr->where ) )
  321.                 if ( window == gSettingsDLOG )
  322.                 {
  323.                     HideWindow( window );
  324.                     menu = GetMHandle( mFile );
  325.                     EnableItem( menu, iSettings );
  326.                 }
  327.             break;
  328.     }
  329. }
  330.  
  331.  
  332. /****************** HandleMenuChoice ***********************/
  333.  
  334. void    HandleMenuChoice( long menuChoice )
  335. {
  336.     short    menu;
  337.     short    item;
  338.     
  339.     if ( menuChoice != 0 )
  340.     {
  341.         menu = HiWord( menuChoice );
  342.         item = LoWord( menuChoice );
  343.         
  344.         switch ( menu )
  345.         {
  346.             case mApple:
  347.                 HandleAppleChoice( item );
  348.                 break;
  349.             case mFile:
  350.                 HandleFileChoice( item );
  351.                 break;
  352.         }
  353.         HiliteMenu( 0 );
  354.     }
  355. }
  356.  
  357.  
  358. /****************** HandleAppleChoice ***********************/
  359.  
  360. void    HandleAppleChoice( short item )
  361. {
  362.     MenuHandle    appleMenu;
  363.     Str255        accName;
  364.     short        accNumber;
  365.     
  366.     switch ( item )
  367.     {
  368.         case iAbout:
  369.             NoteAlert( kAboutALRTid, NULL );
  370.             break;
  371.         default:
  372.             appleMenu = GetMHandle( mApple );
  373.             GetItem( appleMenu, item, accName );
  374.             accNumber = OpenDeskAcc( accName );
  375.             break;
  376.     }
  377. }
  378.  
  379.  
  380. /****************** HandleFileChoice ***********************/
  381.  
  382. void    HandleFileChoice( short item )
  383. {
  384.     short    newPICTid;
  385.     
  386.     switch ( item )
  387.     {
  388.         case iSettings:
  389.             if ( gSettingsDLOG == NULL )
  390.                 CreateDialog();
  391.             else
  392.             {
  393.                 ShowWindow( gSettingsDLOG );
  394.                 SelectWindow( gSettingsDLOG );
  395.             }
  396.             break;
  397.         case iQuit:
  398.             gDone = true;
  399.             break;
  400.     }
  401. }
  402.  
  403.  
  404. /************************************* DoUpdate     */
  405.  
  406. void    DoUpdate( EventRecord *eventPtr )
  407. {
  408.     PicHandle    pic;
  409.     WindowPtr    window;
  410.     Rect        r;
  411.     
  412.     window = (WindowPtr)eventPtr->message;
  413.     
  414.     pic = LoadPICT( gCurrentPICT );
  415.     
  416.     SetPort( window );
  417.     
  418.     BeginUpdate( window );
  419.     
  420.     r = window->portRect;
  421.     DrawPicture( pic, &r );
  422.     
  423.     EndUpdate( window );
  424. }
  425.  
  426.  
  427. /************************************* CreateDialog     */
  428.  
  429. void    CreateDialog( void )
  430. {
  431.     short        itemType;
  432.     Handle        itemHandle;
  433.     Rect        itemRect;
  434.     short        curRadioButton;
  435.  
  436.     gSettingsDLOG = GetNewDialog( kDialogResID, NULL, kMoveToFront );
  437.  
  438.     if ( gSettingsDLOG == NULL )
  439.     {
  440.         SysBeep( 10 );    /*  Couldn't load the DLOG resource!!!  */
  441.         ExitToShell();
  442.     }
  443.     
  444.     ShowWindow( gSettingsDLOG );
  445.     SetPort( gSettingsDLOG );
  446.     
  447.     curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
  448.     GetDItem( gSettingsDLOG, curRadioButton, &itemType, &itemHandle, &itemRect );
  449.     SetCtlValue( (ControlHandle)itemHandle, kOn );
  450. }
  451.  
  452.  
  453. /************************************* FlipControl     */
  454.  
  455. void    FlipControl( ControlHandle control )
  456. {
  457.     SetCtlValue( control, ! GetCtlValue( control ) );
  458. }
  459.  
  460.  
  461. /************************************* SwitchPICT     */
  462.  
  463. void    SwitchPICT( void )
  464. {
  465.     DisposeWindow( gFredWindow );
  466.     
  467.     CreateWindow();
  468. }